package com.hxd.otc.module.my.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.hxd.otc.R;
import com.hxd.otc.base.LazyFragment;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.my.activity.BankAeraChooseActivity;
import com.hxd.otc.module.my.adapter.CurrencyAdapter;
import com.hxd.otc.module.my.bean.CurrencyBean;
import com.hxd.otc.module.my.contract.AddBankCardContract;
import com.hxd.otc.module.my.presenter.AddBankCardPresenter;
import com.hxd.otc.widget.EditInfoInputView;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

/**
 * Created by lzy on 2018/6/8 0008.
 * description ${添加银行卡}
 */
public class AddBankCardFragment extends LazyFragment<AddBankCardPresenter> implements AddBankCardContract.View {

    @BindView(R.id.editInfoInputView_name)
    EditInfoInputView mEditInfoInputViewName;
    @BindView(R.id.editInfoInputView_deposit_bank)
    EditInfoInputView mEditInfoInputViewDepositBank;
    @BindView(R.id.editInfoInputView_open_account_address)
    EditInfoInputView mEditInfoInputViewOpenAccountAddress;
    @BindView(R.id.editInfoInputView_credit_card_numbers)
    EditInfoInputView mEditInfoInputViewCreditCardNumbers;
    @BindView(R.id.tv_support_the_currency)
    TextView mTvSupportTheCurrency;
    @BindView(R.id.rv_support_currency)
    RecyclerView mRvSupportCurrency;
    @BindView(R.id.editInfoInputView_for_note)
    EditInfoInputView mEditInfoInputViewForNote;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.tv_choose_area)
    TextView tvChooseArea;
    private CurrencyAdapter mAdapter;

    @Override
    protected void initView() {

        mEditInfoInputViewName.setInputText(UserHelper.getRealName());
        mRvSupportCurrency.setLayoutManager(new GridLayoutManager(getContext(), 4));
        mAdapter = new CurrencyAdapter(getContext());
        mRvSupportCurrency.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.getCurrecies();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_add_bank_card;
    }

    @Override
    protected AddBankCardPresenter createPresenter() {
        return new AddBankCardPresenter(this);
    }

    public static AddBankCardFragment getInstance() {
        return new AddBankCardFragment();
    }

    @Override
    public void updateCurrencies(List<CurrencyBean> currencies) {
        mAdapter.addAll(true, currencies);
    }

    @Override
    public void addBankCardSuccess(String msg) {
        showToast(msg);
        getActivity().setResult(RESULT_OK);
        getActivity().finish();
    }

    @OnClick(R.id.tv_confirm)
    public void onClick() {
        if (TextUtils.isEmpty(tvChooseArea.getText().toString())) {
            showToast(getString(R.string.choose_country_or_region));
            return;
        }

        String name = mEditInfoInputViewName.getText();
        if (TextUtils.isEmpty(name)) {
            showToast(mEditInfoInputViewName.getHint());
            return;
        }

        if (!UserHelper.isSilver() && !UserHelper.isMerchant()) {//
            if (!name.equals(UserHelper.getRealName())) {
                showToast(getString(R.string.real_name_and_name_not_complay));
                return;
            }
        }

        String bankName = mEditInfoInputViewDepositBank.getText();
        if (TextUtils.isEmpty(bankName)) {
            showToast(mEditInfoInputViewDepositBank.getHint());
            return;
        }

        String bankAddress = mEditInfoInputViewOpenAccountAddress.getText();
        if (TextUtils.isEmpty(bankAddress)) {
            showToast(mEditInfoInputViewOpenAccountAddress.getHint());
            return;
        }
        String bankNo = mEditInfoInputViewCreditCardNumbers.getText();
        if (TextUtils.isEmpty(bankNo)) {
            showToast(mEditInfoInputViewCreditCardNumbers.getHint());
            return;
        }

        String remark = mEditInfoInputViewForNote.getText();

        StringBuilder sb = new StringBuilder();
        for (CurrencyBean item : mAdapter.getItems()) {
            if (item.isSelected())
                sb.append(item.getCode()).append(',');
        }

        if (TextUtils.isEmpty(sb)) {
            showToast(getString(R.string.my_please_select_the_supporting_currency));
            return;
        }

        mPresenter.addBankCard(bankNo, bankName, bankAddress, tvChooseArea.getText().toString().trim(), name, sb.substring(0, sb.length() - 1), remark);
    }

    @OnClick(R.id.cl_choose_area)
    public void onClickArea() {
        Bundle bundle = new Bundle();
        bundle.putBoolean("isAddBank", true);
        $startActivityForResult(BankAeraChooseActivity.class, 123, bundle, false);
    }

    @Override
    protected void initEvent() {
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == 123) {
            tvChooseArea.setText(data.getStringExtra(BankAeraChooseActivity.RESULT_EXTRA_AERA));
        }
    }

}

